package com.kingdee.eas.custom.common;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils extends
		com.kingdee.eas.base.license.expirecheck.DateUtil {

	public static int getMonthBetweenDate(Date d1, Date d2) {
		int beginYear = d1.getYear();
		int beginMonth = d1.getMonth();

		int endYear = d2.getYear();
		int endMonth = d2.getMonth();

		int difMonth = (endYear - beginYear) * 12 + (endMonth - beginMonth);

		return difMonth;

	}

	public final static String FORMAT_YMD = "yyyy-MM-dd";

	public static String getCurrentDateStr() {
		Date d = new Date();
		SimpleDateFormat f = new SimpleDateFormat(FORMAT_YMD);
		return f.format(d);
	}

	public static String date2String(Date d, String format) {
		SimpleDateFormat f = new SimpleDateFormat(format);
		return f.format(d);
	}

	public static int getDaysMonth(int year, int m) {
		GregorianCalendar c = new GregorianCalendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, m - 1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		return c.getActualMaximum(Calendar.DATE);
	}

	public static int getDaysBetweenDate(Date d1, Date d2) {
		GregorianCalendar c1 = new GregorianCalendar();
		GregorianCalendar c2 = new GregorianCalendar();

		c1.setTime(d1);
		c2.setTime(d2);

		c1.set(Calendar.HOUR_OF_DAY, 0);
		c1.set(Calendar.MINUTE, 0);
		c1.set(Calendar.SECOND, 0);
		c1.set(Calendar.MILLISECOND, 0);

		c2.set(Calendar.HOUR_OF_DAY, 0);
		c2.set(Calendar.MINUTE, 0);
		c2.set(Calendar.SECOND, 0);
		c2.set(Calendar.MILLISECOND, 0);

		long l1 = c1.getTimeInMillis();
		long l2 = c2.getTimeInMillis();

		double d = (l2 - l1) / (1000D * 3600D * 24D);
		return MathUtils.roundUp(BigDecimal.valueOf(d), 0).intValue();
	}

	public static double getHourBetweenDate(Date d1, Date d2) {
		GregorianCalendar c1 = new GregorianCalendar();
		GregorianCalendar c2 = new GregorianCalendar();

		c1.setTime(d1);
		c2.setTime(d2);

		long l1 = c1.getTimeInMillis();
		long l2 = c2.getTimeInMillis();

		double h = Long.valueOf((l2 - l1)).doubleValue() / (1000 * 3600);
		return Math.abs(h);
	}

	public static double getMillSecondBetweenDate(Date d1, Date d2) {
		GregorianCalendar c1 = new GregorianCalendar();
		GregorianCalendar c2 = new GregorianCalendar();

		c1.setTime(d1);
		c2.setTime(d2);

		long l1 = c1.getTimeInMillis();
		long l2 = c2.getTimeInMillis();
		return Math.abs(l1 - l2);
	}

	public static Date getDateMax(Date d) {
		Calendar c = Calendar.getInstance();

		Date date = DateUtils.addDate(d, 1);
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);

		return c.getTime();

	}

	public static Date getDateMin(Date d) {
		Calendar c = Calendar.getInstance();

		c.setTime(d);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);

		return c.getTime();
	}
}
